package com.cc.gulimall.order.web;


import com.alipay.api.AlipayApiException;
import com.cc.gulimall.order.config.AlipayTemplate;
import com.cc.gulimall.order.service.OrderService;
import com.cc.gulimall.order.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>Title: PayWebController</p>
 * Description：
 * date：2020/7/4 19:59
 */

@Controller
public class PayWebController {

    @Autowired
    private AlipayTemplate alipayTemplate;

    @Autowired
    private OrderService orderService;


    /**
     * 告诉浏览器我们会返回一个html页面, 是支付宝响应给我们的
     * 支付成功后，我们要跳到订单列表页，配置好url
     * <p>
     * <p>
     * 接受是从pay页面提交的请求。选付款方式那个页面
     * th:href="'http://order.gulimall.com/payOrder?orderSn=' + ${submitOrderResp.orderEntity.orderSn}">支付宝</a>
     */

    @ResponseBody
    @GetMapping(value = "/payOrder", produces = "text/html")/*响应一个html页面, 是支付宝响应给我们的*/
    public String payOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {

        /*封装支付用的订单简略信息，支付宝规范，pay方法要用*/
        PayVo payVo = orderService.getOrderPay(orderSn);
        /*调用阿里支付接口，发请求，支付宝给响应，是二维码付款页面，直接交给浏览器*/
        String pay = alipayTemplate.pay(payVo);
        return pay;
    }
}

